package top.lyjwn.todo.weinxin.api.bjnews;

import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.lyjwn.todo.weinxin.api.url.WXBjnewsUrl;
import top.lyjwn.todo.weinxin.bjnews.utils.Lang;
import top.lyjwn.todo.weinxin.data.bjnews.BjnewsUserAccessToken;
import top.lyjwn.todo.common.utils.http.HttpUtil;

/**
 * 拉取用户信息(需scope为 snsapi_userinfo)
 * <p>
 * 如果网页授权作用域为snsapi_userinfo，则此时开发者可以通过access_token和openid拉取用户信息了。
 *
 * @author luoyujie
 */
@Component
public class PullWXUserInfo {

    @Autowired
    private WXBjnewsUrl wxBjnewsUrl;


    public PullWXUserInfo() {
    }


    public String getUrl(BjnewsUserAccessToken accessToken, String lang) {
        String url = wxBjnewsUrl.getPULL_WXUSER_INFO().replace("#{ACCESS_TOKEN}", accessToken.getAccessToken()).replace("#{OPENID}", accessToken.getOpenid()).replace("#{LANG}", lang);
        return url;
    }

    public String getUrl(BjnewsUserAccessToken accessToken) {
        String url = wxBjnewsUrl.getPULL_WXUSER_INFO().replace("#{ACCESS_TOKEN}", accessToken.getAccessToken()).replace("#{OPENID}", accessToken.getOpenid()).replace("#{LANG}", Lang.ZH_CN);
        return url;
    }


    public String getResultString(BjnewsUserAccessToken accessToken, String lang) {
        String result = HttpUtil.request(this.getUrl(accessToken, lang), HttpUtil.METHOD_GET);
        return result;
    }

    public JSONObject getResultJSONObject(BjnewsUserAccessToken accessToken, String lang) {
        String resultString = this.getResultString(accessToken, lang);
        return JSONObject.parseObject(resultString);
    }


    public String getResultString(BjnewsUserAccessToken accessToken) {
        String result = HttpUtil.request(this.getUrl(accessToken), HttpUtil.METHOD_GET);
        return result;
    }

    public JSONObject getResultJSONObject(BjnewsUserAccessToken accessToken) {
        String resultString = this.getResultString(accessToken);
        return JSONObject.parseObject(resultString);
    }


}
